import User from '@/utils/xMessager/User'
import { arrayInsert, arrayRemove } from '@/utils'
import { Base64 } from './xchat/base64'

function OnChat(chatdata, { roompara, chatDisplay, xmessager, store, chat, header, mode }) {
	if (roompara.room_id != chatdata.frid && !chatdata.secret) {
	  // 不接收其它房间公聊信息
	  if (roompara.is_receive_share == 0) {
	    return false
	  }
	  // 仅接受主房间公聊信息
	  if (roompara.is_receive_share == 1 && roompara.parent_room_id != chatdata.frid) {
	    return false
	  }
	}
  let is_my_message = false
  if (chatdata.from_uid == xmessager.uid) {
    is_my_message = true
    chatdata.from_nickname = '我'
    chatdata.is_my = true
  }
  if (chatdata.to_uid == xmessager.uid) {
    is_my_message = true
    chatdata.to_nickname = '我'
    chatdata.is_my_to = true
  }

  if (chatdata.secret) {
		chatdata.time = new Date().getTime()
    const { privateChatMap } = store.getters
    let privateChatArr = []
    let privateChatUserArr = store.getters.privateChatArray
    let other_uid
    if (chatdata.from_uid == xmessager.uid && chatdata.to_uid != xmessager.uid) {
      other_uid = chatdata.to_uid
    }
    if (chatdata.to_uid == xmessager.uid && chatdata.from_uid != xmessager.uid) {
      other_uid = chatdata.from_uid
    }
    if (privateChatUserArr.length < 100) {
      if (privateChatUserArr.length > 0) {
        const chatuserindex = privateChatUserArr.findIndex(item => item.uid == other_uid)
        if (chatuserindex !== -1) {
          // 将最新聊天的用户放在列表第一位
          const user = privateChatUserArr[chatuserindex]
          arrayRemove(privateChatUserArr, chatuserindex)
          if (user) {
            arrayInsert(privateChatUserArr, 0, user)
          }
        } else {
          const user = chat.getUser(parseInt(other_uid))
          if (user) {
            arrayInsert(privateChatUserArr, 0, user)
          }
        }
      } else {
        const user = chat.getUser(parseInt(other_uid))
        if (user) {
          arrayInsert(privateChatUserArr, 0, user)
        }
      }
    }
    privateChatUserArr = [...new Set(privateChatUserArr)]
    // 统一私聊数组
    chat.private_chat_array = privateChatUserArr
    store.commit('SET_PRIVATECHATARRAY', privateChatUserArr)
    if (privateChatMap[other_uid]) {
      privateChatArr = privateChatMap[other_uid]
    }
		chatDisplay.filterChatdataMessage(chatdata)
		if(!privateChatArr.length){
			chatdata.showTime = true
		}else{
			let privateItem
			privateChatArr.map((item,index)=>{
				item.showTime && (privateItem = item)
			})
			console.log(chatdata.time -  privateItem.time)
			if( privateItem && chatdata.time -  privateItem.time > 60 * 1000){
				chatdata.showTime = true
			}
		}
    privateChatArr.push(chatdata)
    privateChatMap[other_uid] = privateChatArr
    store.commit('SET_PRIVATECHATMAP', { ...privateChatMap })
    if (!mode) {
      // 设置私聊提醒
      if (header) {
        if ((!header.privateFlag && !header.privateDetail) || (!header.privateFlag && header.privateDetail && other_uid != header.selectedItem.uid)) {
          header.privateStartTips = true
        }
      }
    }
    if (header) {
      if (header.selectedItem) {
        if (other_uid != header.selectedItem.uid) {
          const item = header.privateChatArray.find(item => other_uid == item.uid)
          if (item && !item.messageFlag) {
            item.messageFlag = true
          }
        }
      } else {
        const item = header.privateChatArray.find(item => other_uid == item.uid)
        if (item && !item.messageFlag) {
          item.messageFlag = true
        }
      }
    }
  } else {
    if (chatdata.title > 12) {
      chatdata.title = 12
    }
    chatDisplay.common_chat_display_chatmessage(chatdata, is_my_message)
  }
}
function OnGift(data, { roompara, chatDisplay, xmessager, chat }) {
	console.log('OnGift:',data)
  let is_my_message = false
  if (data.from_uid == xmessager.uid || data.to_uid == xmessager.uid) {
    is_my_message = true
  }
  data.fromu = chat.getUser(data.from_uid)
  if (data.to_uid) {
    data.tou = chat.getUser(data.to_uid)
  }
  chatDisplay.disgift(data, is_my_message)
  chatDisplay.common_insert(
    `<span style="color:#4360F0;margin-right:3px;">${data.from_nickname}</span>给<span style="color:#DA0A0A;margin:0 3px;">${data.to_nickname}</span>送了${data.sum}个${data.gift_name}`,
    1
  )
}
// 公聊删除
function OnPublic_Msg_Del(data, { chatDisplay }) {
  chatDisplay.deleteInfo(data.msg_id)
}
function setTips(param, { chatDisplay, xmessager, u }, levelTips) {
  if (param.op == 1) {
    if (xmessager.uid == param.uid) {
      chatDisplay.common_insert(`<span class="warning">嘿，你被设置成${levelTips}咯，请刷新页面或退出房间重试！</span>`, 1)
    } else if (xmessager.level >= 500) {
      chatDisplay.common_insert(`<span class="warning">嘿，${u.appdata.nickname}被设置成${levelTips}咯！</span>`, 1)
    }
  } else if (xmessager.uid == param.uid) {
    chatDisplay.common_insert(`<span class="warning">你不再是${levelTips}了，请刷新页面或退出房间重试！</span>`, 1)
  } else if (xmessager.level >= 500) {
    chatDisplay.common_insert(`<span class="warning">${u.appdata.nickname}的${levelTips}身份被下了!</span>`, 1)
  }
}
// 设置管理员
function OnSetAdmin(param, { chatDisplay, xmessager, chat }) {
  const u = chat.getUser(param.uid)
  if (!u) {
    return
  }
  switch (param.type) {
    case 900:
      setTips(param, { chatDisplay, xmessager, chat, u }, '管理')
      break
    case 500:
      setTips(param, { chatDisplay, xmessager, chat, u }, '讲师')
      break
    default:
      console.log('还没做呢亲')
      break
  }
}
// 设置管理员
function OnSetTeacher(param, { chatDisplay, xmessager, chat }) {
  if (param.code == 1) {
    if (xmessager.uid == param.to_uid) {
      chatDisplay.common_insert('<span class="warning">嘿，你被设置成讲师咯！</span>', 1)
    } else {
      chatDisplay.common_insert(`<span class="warning">嘿，${param.to_nickname}被设置成讲师咯！</span>`, 1)
    }
  } else if (xmessager.uid == param.to_uid) {
    chatDisplay.common_insert('<span class="warning">你不再是讲师了！</span>', 1)
  } else {
    chatDisplay.common_insert(`<span class="warning">${param.to_nickname}管理被下了!</span>`, 1)
  }
}
// 封账号、IP
function OnBlackIpList(param, { chatDisplay, xmessager }) {
  if (param.uid == xmessager.uid) {
    chatDisplay.common_insert('<span class="warning">让你捣乱！你被封了！</span>', 1)
    setTimeout(() => {
      location.reload(true)
    }, 1000)
  } else {
    const s = `<span class="warning">${param.msg}</span>`
    chatDisplay.common_insert(s, 1)
  }
}
// 设置接不接受主房间消息
function OnUPDATE_RECEIVE(data, { roompara, store }) {
  roompara.is_receive_share = data.is_receive_share
  store.dispatch('setRoomPara', roompara)
}
export function chatDataHandle(data, roompara) {
  switch (data.type) {
    case 'C':
      OnChat(data.data, roompara)
      break
    // 送礼
    case 'G':
      OnGift(data.data, roompara)
      break
    // 删除公聊
    case 'PUBLIC_MSG_DEL':
      OnPublic_Msg_Del(data.data, roompara)
      break
    // 设置管理员
    case 'ROOM_SET_ADMIN':
      OnSetAdmin(data.data, roompara)
      break
    // 设置讲师
    case 'ROOM_SET_TEACHER':
      OnSetTeacher(data.data, roompara)
      break
    // 封账号、IP
    case 'IP':
      OnBlackIpList(data.data, roompara)
      break
    // 设置接不接受主房间消息
    case 'UPDATE_RECEIVE':
      OnUPDATE_RECEIVE(data.data, roompara)
      break
    default:
      console.log('还没做呢亲')
      break
  }
}
